package com.wang.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wang.mall.bean.OmsOrder;
import com.wang.mall.model.R;
import com.wang.mall.service.OmsOrderService;
import com.wang.mall.vo.OrderConfirmVo;
import com.wang.mall.vo.OrderSubmitVo;
import com.wang.mall.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OmsOrderService omsOrderService;


    @GetMapping("/getCurrentUserList")
    public R<List<OmsOrder>> getCurrentUserList() {
        List<OmsOrder> omsOrderList = omsOrderService.getCurrentUserList();
        return R.ok(omsOrderList);
    }

    @GetMapping("/getOrderByOrdersn")
    public R<OmsOrder> getOrderByOrdersn(String orderSn) {
        OmsOrder omsOrder = omsOrderService.getOne(new LambdaQueryWrapper<OmsOrder>().eq(OmsOrder::getOrderSn, orderSn));

        return R.ok(omsOrder);
    }

    /**
     * 提交订单
     *
     * @param submitVo
     * @return
     */
    @PostMapping("/submitOrder")
    public R submitOrder(@RequestBody OrderSubmitVo submitVo) {
        SubmitOrderResponseVo responseVo = omsOrderService.submitOrder(submitVo);
        Integer code = responseVo.getCode();
        if (code == 0) {
            return R.ok(responseVo);
        }
        if (code == 3) {
            return R.fail("订单请勿重复提交");
        } else if (code == 1) {
            return R.fail("商品库存不足");
        } else if (code == 2) {
            return R.fail("价格对比失败");
        }
        return R.fail("未知异常");
    }

    /**
     * 构建订单确认页 的数据
     *
     * @return
     */
    @GetMapping("/generateOrder")
    public R generateOrder() {
        OrderConfirmVo o = omsOrderService.generateOrder();
        return R.ok(o);
    }
}
